package ru.bytedog.oiot.counter.bot.service;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.methods.updatingmessages.DeleteMessage;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import ru.bytedog.oiot.counter.bot.bean.BotAnswer;
import ru.bytedog.oiot.counter.bot.config.BotConfigurationTest;
import ru.bytedog.oiot.counter.bot.service.command.CommandProcessor;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

@SpringBootTest
@Import(BotConfigurationTest.class)
class TelegramBotTest {

    @MockBean
    CommandProcessor processor;
    @Autowired
    TelegramBot bot;

    @Test
    void onUpdateReceived() throws TelegramApiException {
        Update botUpdate = new Update();
        bot.onUpdateReceived(botUpdate);

        Message message = new Message();
        botUpdate.setMessage(message);
        bot.onUpdateReceived(botUpdate);
        verify(processor, times(0)).processMessage(any());

        message.setText("test_message");
        BotAnswer answer = new BotAnswer();
        answer.setChatId("chat");
        answer.setDeleteMessageId(1);
        answer.setMessage("message");
        when(processor.processMessage(any())).thenReturn(answer);

        TelegramBot spyBot = spy(bot);
        doReturn(true).when(spyBot).execute(any(DeleteMessage.class));
        doReturn(new Message()).when(spyBot).execute(any(SendMessage.class));

        spyBot.onUpdateReceived(botUpdate);
        verify(processor, times(1)).processMessage(any());
    }

    @Test
    void getBotUsername() {
        assertEquals("test_bot_name", bot.getBotUsername());
    }
}